package com.stone.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.stone.model.Admin;
import com.stone.model.Provider;
import com.stone.model.Rank;
import com.stone.model.Recommend;
import com.stone.model.Stone;
import com.stone.service.IProviderService;
import com.stone.service.IRankService;
import com.stone.service.IStoneService;
import com.stone.utils.LogConstrants;
import com.stone.utils.Page;

public class RankAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5003003575318308214L;

	private int rankflag;
	
	private String category;
	
	private String error;
	
	private IStoneService stoneService;
	
	private IProviderService providerService;
	
	private Page page;
	
	private List<Stone> stonesList;
	
	private int listSize;
	
	private List<Stone> alreadyRankedStonesList;
	
	private List<Stone> alreadyRankedStonesList1;
	
	private List<Provider> providersList;
	
	private List<Provider> alreadyRankedProvidersList;
	
	private IRankService rankService;
	
	private String selectedStones;
	
	private String selectedProviders;
	
	public IRankService getRankService() {
		return rankService;
	}

	public void setRankService(IRankService rankService) {
		this.rankService = rankService;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}
	
	public IStoneService getStoneService() {
		return stoneService;
	}

	public void setStoneService(IStoneService stoneService) {
		this.stoneService = stoneService;
	}

	public IProviderService getProviderService() {
		return providerService;
	}

	public void setProviderService(IProviderService providerService) {
		this.providerService = providerService;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public int getRankflag() {
		return rankflag;
	}

	public void setRankflag(int rankflag) {
		this.rankflag = rankflag;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public List<Stone> getStonesList() {
		return stonesList;
	}

	public void setStonesList(List<Stone> stonesList) {
		this.stonesList = stonesList;
	}

	public List<Stone> getAlreadyRankedStonesList() {
		return alreadyRankedStonesList;
	}

	public void setAlreadyRankedStonesList(List<Stone> alreadyRankedStonesList) {
		this.alreadyRankedStonesList = alreadyRankedStonesList;
	}

	public List<Stone> getAlreadyRankedStonesList1() {
		return alreadyRankedStonesList1;
	}

	public void setAlreadyRankedStonesList1(List<Stone> alreadyRankedStonesList1) {
		this.alreadyRankedStonesList1 = alreadyRankedStonesList1;
	}

	public List<Provider> getProvidersList() {
		return providersList;
	}

	public void setProvidersList(List<Provider> providersList) {
		this.providersList = providersList;
	}

	public List<Provider> getAlreadyRankedProvidersList() {
		return alreadyRankedProvidersList;
	}

	public void setAlreadyRankedProvidersList(
			List<Provider> alreadyRankedProvidersList) {
		this.alreadyRankedProvidersList = alreadyRankedProvidersList;
	}

	public int getListSize() {
		return listSize;
	}

	public void setListSize(int listSize) {
		this.listSize = listSize;
	}
	
	public String getSelectedStones() {
		return selectedStones;
	}

	public void setSelectedStones(String selectedStones) {
		this.selectedStones = selectedStones;
	}

	public String getSelectedProviders() {
		return selectedProviders;
	}

	public void setSelectedProviders(String selectedProviders) {
		this.selectedProviders = selectedProviders;
	}

	public String getStoneListAjax(){
	
		if(rankflag == 1)
		{
			page = stoneService.findStonePageByPurpose(category, 1, 50);
		}
		else
		{
			page = stoneService.findStonePageByColor(category, 1, 50);
		}
		
		stonesList = page.getList();
		alreadyRankedStonesList = new ArrayList<Stone>();
		Rank rank = rankService.findRankByType(category);
		if(rank != null)
		{
			if(rank.getRank() != null && ! rank.getRank().equals("")) 
			{
				String[] rIds = rank.getRank().split(";");
				for(String rId : rIds) {
					Stone stone = stoneService.findStoneById(Long.parseLong(rId));
					stonesList.remove(stone);
					alreadyRankedStonesList.add(stone);
				}
			}
		}
		listSize = stonesList.size();
		return "success";
	}
	
	public String saveRank() {
		Rank rank = rankService.findRankByType(category);
		boolean isSave = false;
		if(rank == null) {
			rank = new Rank();
			isSave = true;
		}
		if(category.equals("供应商"))
			rank.setRank(selectedProviders);
		else
			rank.setRank(selectedStones);
		rank.setType(category);
		if(isSave)
			rankService.addRank(rank);
		else
			rankService.updateRank(rank);
		return "success";
	}
	
	public String listProviders(){
		providersList = providerService.findAllProviders(1, 50).getList();// 用于显示的供应商列表
		alreadyRankedProvidersList = new ArrayList<Provider>();
		Rank rank = rankService.findRankByType(category);
		if(rank != null)
		{
			if(rank.getRank() != null && ! rank.getRank().equals("")) 
			{
				String[] rIds = rank.getRank().split(";");
				for(String rId : rIds) {
					Provider provider = providerService.findProviderById(Long.parseLong(rId));
					providersList.remove(provider);
					alreadyRankedProvidersList.add(provider);
				}
			}
		}
		listSize = providersList.size();
		return "success";
	}
	
	public String listRank()
	{
		alreadyRankedProvidersList = new ArrayList<Provider>();
		Rank rank = rankService.findRankByType("供应商");
		if(rank != null)
		{
			if(rank.getRank() != null && ! rank.getRank().equals("")) 
			{
				String[] rIds = rank.getRank().split(";");
				for(String rId : rIds) {
					Provider provider = providerService.findProviderById(Long.parseLong(rId));
					alreadyRankedProvidersList.add(provider);
				}
			}
		}
		page = stoneService.findStonePageByColor("白色", 1, 50);
		stonesList = page.getList();
		alreadyRankedStonesList = new ArrayList<Stone>();
		rank = rankService.findRankByType("白色");
		if(rank != null)
		{
			if(rank.getRank() != null && ! rank.getRank().equals("")) 
			{
				String[] rIds = rank.getRank().split(";");
				for(String rId : rIds) {
					Stone stone = stoneService.findStoneById(Long.parseLong(rId));
					stonesList.remove(stone);
					alreadyRankedStonesList.add(stone);
				}
			}
		}
		
		page = stoneService.findStonePageByPurpose("室内地面", 1, 50);
		stonesList = page.getList();
		alreadyRankedStonesList1 = new ArrayList<Stone>();
		rank = rankService.findRankByType("室内地面");
		if(rank != null)
		{
			if(rank.getRank() != null && ! rank.getRank().equals("")) 
			{
				String[] rIds = rank.getRank().split(";");
				for(String rId : rIds) {
					Stone stone = stoneService.findStoneById(Long.parseLong(rId));
					stonesList.remove(stone);
					alreadyRankedStonesList1.add(stone);
				}
			}
		}
		
		return "success";
	}
}
